package cn.scy.business.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import cn.scy.annotation.PassToken;
import cn.scy.base.PageParam;
import cn.scy.base.PageResult;
import cn.scy.base.ResponseDTO;
import cn.scy.business.domain.entity.SysUser;
import cn.scy.business.domain.param.ChangePwdParam;
import cn.scy.business.domain.param.LoginParam;
import cn.scy.business.domain.vo.SysUserVO;
import cn.scy.business.service.GetTokenService;
import cn.scy.business.service.UserLoginService;
import cn.scy.constant.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户相关接口
 *
 * @author scy
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserLoginController {

    @Resource
    private UserLoginService userLoginService;

    @Resource
    private GetTokenService getTokenService;

    /**
     * 账号密码登录
     *
     * @param loginParam 登录参数
     * @param request    request
     * @return 登录结果
     */
    @PassToken
    @PostMapping("login")
    public ResponseDTO<Map<String, Object>> login(@RequestBody LoginParam loginParam, HttpServletRequest request) {
        log.info("账号密码登录" + JSONUtil.toJsonStr(loginParam) + " ，用户IP：" + request.getRemoteAddr());
        if (StrUtil.isBlank(loginParam.getLoginId()) || StrUtil.isBlank(loginParam.getPasswd())) {
            return ResponseDTO.error(ErrorCode.LOGIN_ERR);
        }
        if (!userLoginService.checkPasswd(loginParam)) {
            log.info(loginParam.getLoginId() + " 登录失败，密码错误");
            return ResponseDTO.error(ErrorCode.LOGIN_ERR);
        }

        log.info(loginParam.getLoginId() + " 登录成功");
        String accessToken = userLoginService.login(loginParam);
        if (accessToken == null) {
            return ResponseDTO.error(ErrorCode.LOGIN_ERR);
        }
        Map<String, Object> back = new HashMap<>(1);
        back.put("accessToken", accessToken);
        return ResponseDTO.ok(back);
    }

    /**
     * 分页查询用户列表
     *
     * @param pageParam 分页参数
     * @return 用户列表
     */
    @GetMapping("listPage")
    public ResponseDTO<PageResult<SysUserVO>> listPage(PageParam pageParam) {
        return ResponseDTO.ok(userLoginService.listPage(pageParam));
    }

    /**
     * 查询当前已登录用户
     *
     * @param request request
     * @return 用户信息
     */
    @GetMapping("getSysUser")
    public ResponseDTO<SysUserVO> getSysUser(HttpServletRequest request) {
        SysUser userByToken = getTokenService.getUserByToken(request);
        return ResponseDTO.ok(BeanUtil.copyProperties(userByToken, SysUserVO.class));
    }

    /**
     * 注销
     *
     * @param request request
     * @return 注销结果
     */
    @PostMapping("loginOut")
    public ResponseDTO<Boolean> loginOut(HttpServletRequest request) {
        String userId = getTokenService.getUserIdByToken(request);
        return ResponseDTO.ok(userLoginService.loginOut(userId));
    }

    /**
     * 修改密码
     *
     * @param changePwdParam 修改密码参数
     * @param request        request
     * @return 修改密码结果
     */
    @PostMapping("changePwd")
    public ResponseDTO<Boolean> changePwd(@RequestBody @Valid ChangePwdParam changePwdParam, HttpServletRequest request) {
        return ResponseDTO.ok(userLoginService.changePwd(changePwdParam, request));
    }

    /**
     * 重置密码
     *
     * @param id 用户ID
     * @return 重置密码结果
     */
    @PostMapping(path = "resetPwd")
    public ResponseDTO<Boolean> resetPwd(@RequestParam(value = "id") String id) {
        return ResponseDTO.ok(userLoginService.resetPwd(id));
    }

}
